// pages/caigouList/caigouList.js
var webContext = getApp().globalData.webContext
var index = 1
var result = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noData: false
  },
  loadData: function () {
    var _this = this
    var openid=getApp().globalData.openid;
    if (openid){
      wx.request({
        url: webContext + '/ebridge_h5/caigou/getListByPage',//json数据地址
        data: {
          index: index,
          city: '',
          verity: '',
          proCat: '',
          openId: openid

        },
        headers: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
          if (index > 1) {
            if (res.data.data.purchase.length > 0) {

              for (var i = 0; i < res.data.data.purchase.length; i++) {

                result.push(res.data.data.purchase[i]);

              }
              _this.setData({
                list_data: result
              })
            }
          }else {
            for (var i = 0; i < res.data.data.purchase.length; i++) {

              result.push(res.data.data.purchase[i]);

            }
            _this.setData({
              list_data: result
            })

            if (res.data.data.purchase.length == 0) {
              _this.setData({
                noData: true
              })
            } else {
              _this.setData({
                noData: false
              })
            }
          }
        }
      })
    }else{
      _this.setData({
        noData: true
      })
    }
   
  },
  toDetails: function (e) {
    var id = e.currentTarget.dataset.id;
    console.log(e);
    if (id) {
      wx.navigateTo({
        url: '../../pages/caigouDetails/caigouDetails?id=' + id
      })
    }
  },
  toReleaseCaigou: function (e) {
    wx.navigateTo({
      url: '../../pages/releaseCaigou/releaseCaigou'
    })
  },
  
  deleteCaigou: function (e) {
    var id = e.currentTarget.dataset.id;
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确认删除？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: webContext + '/ebridge_h5/caigou/deleteCaigou',
            data: { id: id },
            headers: {
              'Content-Type': 'application/json'
            },
            success: function (res) {
              console.log(res.data);
              if (res.data.data.collection == '1') {
                result = [];
                _this.loadData();
              } else {
                wx.showModal({
                  title: '提示',
                  content: '删除失败',
                  showCancel: false
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    result=[];
    this.loadData();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    index = 1;
    result=[];
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.loadData();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    index++;
    this.loadData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})